package br.ufrj.dcc.ad;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class ScatterPlotChart {

	private JFreeChart chart;
	
	public ScatterPlotChart(String title, String xLabel, String yLabel, List<Map<Double, Double>> listMap) {
		
		int i = 0;
		final XYSeriesCollection xyCollection = new XYSeriesCollection();
		for (Map<Double, Double> map : listMap) {
			final XYSeries xySeries = new XYSeries("Session "+i);
			for (Entry<Double, Double> entry : map.entrySet()) {
				xySeries.add(entry.getKey(), entry.getValue());
			}
			xyCollection.addSeries(xySeries);
			i++;
		}

		this.chart = ChartFactory.createScatterPlot(title, xLabel, yLabel, xyCollection, PlotOrientation.VERTICAL, false, false, false);
	}
	
	public void saveAsImage(String name) throws IOException {
		final String path = Simulator.class.getResource("Simulator.class").getPath();
		final String filepath = path.substring(0, path.indexOf("target")) + "graphs" + "\\" + name + ".png";
		final File file = new File(filepath);
		ChartUtilities.saveChartAsPNG(file, getChart(), 1600, 900);
	}
	
	public JFreeChart getChart() {
		return chart;
	}

}